import React from 'react';
import { parse } from 'querystring';
import _ from 'lodash';
import moment from 'moment';
import { Tooltip } from 'antd';
import { TextWithTags, NumberView, utils } from '@/components/BaseTable';
import HeaderRenderer from '@/components/BaseTable/HeaderRenderer';
/* eslint no-useless-escape:0 import/prefer-default-export:0 */

export const getArrayNumbers = (text, division = 1) => {
  if (!_.isEmpty(text) && text.indexOf('/') > -1) {
    // text.split('/')
    const temp = [];
    _.forEach(text.split('/'), value => {
      temp.push((_.trim(value) * 1) / division);
    });
    text = temp;
  } else {
    text = (text * 1) / division;
  }

  return text;
};

export const setFixedColumn = (attributesCheckeds, year, type) => {
  const pKeys = ['budgetValue', 'dynamicValue', 'differenceValue'];
  const fixedColumns = [];
  // if 月合计 12 else 1
  let length = 1;
  if (type === 'firstMonthlyValue') length = 6;
  if (type === 'secondMonthlyValue') length = 12;
  const initNum = type === 'secondMonthlyValue' ? 7 : 1;
  for (let i = initNum; i <= length; i += 1) {
    let yearMonthKey = '';
    if (type === 'firstMonthlyValue') yearMonthKey = `${year}-0${i}`;
    if (type === 'secondMonthlyValue') yearMonthKey = `${year}-${i < 10 ? '0' : ''}${i}`;
    for (let j = 0; j < pKeys.length; j += 1) {
      const pKey = pKeys[j];
      if (_.indexOf(attributesCheckeds, '套数') > -1) {
        fixedColumns.push({
          key: `${year}-${type}-${yearMonthKey}-${i + j}-num`,
          dataKey: `${year}-${type}-${yearMonthKey}-${i + j}-num`,
          title: '',
          width: 180,
          align: 'right',
          headerRenderer: () => (
            <HeaderRenderer type={1}>
              <div>套数/车位数</div>
              <div>(套/个)</div>
            </HeaderRenderer>
          ),
          dataGetter: ({ rowData }) => {
            let val = [];
            if (type === 'firstMonthlyValue' || type === 'secondMonthlyValue') {
              const filterData = _.filter(rowData[type], x => x.yearMonth === yearMonthKey)[0];
              if (rowData.type === 'org_product') {
                val = [
                  !rowData.isParking
                    ? filterData[pKey].roomNum * 1
                    : filterData[pKey].parkingNum * 1,
                ];
              } else {
                val = [filterData[pKey].roomNum * 1, filterData[pKey].parkingNum * 1];
              }
            } else if (rowData.type === 'org_product') {
              val = [
                !rowData.isParking
                  ? rowData[type][pKey].roomNum * 1
                  : rowData[type][pKey].parkingNum * 1,
              ];
            } else {
              val = [rowData[type][pKey].roomNum * 1, rowData[type][pKey].parkingNum * 1];
            }
            return val;
          },
          cellRenderer: <NumberView precision={0} />,
        });
      }
      if (_.indexOf(attributesCheckeds, '面积') > -1) {
        fixedColumns.push({
          key: `${year}-${type}-${yearMonthKey}-${i + j}-area`,
          dataKey: `${year}-${type}-${yearMonthKey}-${i + j}-area`,
          title: '',
          width: 180,
          align: 'right',
          headerRenderer: () => (
            <div>
              <div>面积</div>
              <div>(平米)</div>
            </div>
          ),
          dataGetter: ({ rowData }) => {
            let val = [];
            if (type === 'firstMonthlyValue' || type === 'secondMonthlyValue') {
              const filterData = _.filter(rowData[type], x => x.yearMonth === yearMonthKey)[0];
              val = [filterData[pKey].saleArea * 1];
            } else {
              val = [rowData[type][pKey].saleArea * 1];
            }
            return val;
          },
          cellRenderer: <NumberView precision={2} />,
        });
      }
      if (_.indexOf(attributesCheckeds, '均价') > -1) {
        fixedColumns.push({
          key: `${year}-${type}-${yearMonthKey}-${i + j}-averagePrice`,
          dataKey: `${year}-${type}-${yearMonthKey}-${i + j}-averagePrice`,
          title: '',
          width: 180,
          align: 'right',
          headerRenderer: () => (
            <HeaderRenderer type={2}>
              <div>均价</div>
              <div>(元/平米,个)</div>
            </HeaderRenderer>
          ),
          dataGetter: ({ rowData }) => {
            let val = '';
            if (type === 'firstMonthlyValue' || type === 'secondMonthlyValue') {
              const filterData = _.filter(rowData[type], x => x.yearMonth === yearMonthKey)[0];
              if (rowData.type === 'org_product') {
                val = [
                  !rowData.isParking
                    ? filterData[pKey].houseAvgPrice * 1
                    : filterData[pKey].parkingAvgPrice * 1,
                ];
              } else {
                val = [filterData[pKey].houseAvgPrice * 1, filterData[pKey].parkingAvgPrice * 1];
              }
            } else if (rowData.type === 'org_product') {
              val = [
                !rowData.isParking
                  ? rowData[type][pKey].houseAvgPrice * 1
                  : rowData[type][pKey].parkingAvgPrice * 1,
              ];
            } else {
              val = [
                rowData[type][pKey].houseAvgPrice * 1,
                rowData[type][pKey].parkingAvgPrice * 1,
              ];
            }
            return val;
          },
          cellRenderer: <NumberView precision={0} />,
        });
      }
      if (_.indexOf(attributesCheckeds, '金额') > -1) {
        fixedColumns.push({
          key: `${year}-${type}-${yearMonthKey}-${i + j}-price`,
          dataKey: `${year}-${type}-${yearMonthKey}-${i + j}-price`,
          title: '',
          width: 180,
          align: 'right',
          headerRenderer: () => (
            <HeaderRenderer type={3}>
              <div>金额</div>
              <div>(万元：合计/房源/车位)</div>
            </HeaderRenderer>
          ),
          dataGetter: ({ rowData }) => {
            let val = '';
            if (type === 'firstMonthlyValue' || type === 'secondMonthlyValue') {
              const filterData = _.filter(rowData[type], x => x.yearMonth === yearMonthKey)[0];
              if (rowData.type === 'org_product') {
                val = [
                  !rowData.isParking
                    ? filterData[pKey].housePrice / 10000
                    : filterData[pKey].parkingPrice / 10000,
                ];
              } else {
                val = [
                  filterData[pKey].housePrice / 10000 + filterData[pKey].parkingPrice / 10000,
                  filterData[pKey].housePrice / 10000,
                  filterData[pKey].parkingPrice / 10000,
                ];
              }
            } else if (rowData.type === 'org_product') {
              val = [
                !rowData.isParking
                  ? rowData[type][pKey].housePrice / 10000
                  : rowData[type][pKey].parkingPrice / 10000,
              ];
            } else {
              val = [
                rowData[type][pKey].housePrice / 10000 + rowData[type][pKey].parkingPrice / 10000,
                rowData[type][pKey].housePrice / 10000,
                rowData[type][pKey].parkingPrice / 10000,
              ];
            }
            return val;
          },
          cellRenderer: <NumberView precision={0} />,
        });
      }
    }
  }
  return fixedColumns;
};

export const getFixedColumns = (deadline, attributesCheckeds) => {
  const year = deadline.split('-')[0];
  let fixedColumns = [
    {
      key: 'name',
      dataKey: 'name',
      title: '',
      width: 260,
      frozen: 'left',
      headerRenderer: () => <div>分期/组团/业态/楼栋</div>,
      cellRenderer: ({ rowData }) => (
        <Tooltip
          placement="topLeft"
          title={`${rowData.name}${
            rowData.type === 'org_product'
              ? `(${rowData.businessTypeName}+${rowData.freeTypeName}+${rowData.proTypeName})`
              : ''
          }`}
        >
          <div
            style={{
              width: '100%',
              textOverflow: 'ellipsis',
              whiteSpace: 'nowrap',
              overflow: 'hidden',
            }}
          >
            {`${rowData.name}${
              rowData.type === 'org_product'
                ? `(${rowData.businessTypeName}+${rowData.freeTypeName}+${rowData.proTypeName})`
                : ''
            }`}
          </div>
        </Tooltip>
      ),
    },
  ];
  // xxxx年度合计
  fixedColumns = _.concat(fixedColumns, setFixedColumn(attributesCheckeds, year, 'yearValue'));
  // 1-6月份
  fixedColumns = _.concat(
    fixedColumns,
    setFixedColumn(attributesCheckeds, year, 'firstMonthlyValue'),
  );
  // 半年度对比
  fixedColumns = _.concat(
    fixedColumns,
    setFixedColumn(attributesCheckeds, year, 'firstHalfYearValue'),
  );
  // 下半年计划
  fixedColumns = _.concat(
    fixedColumns,
    setFixedColumn(attributesCheckeds, year, 'secondHalfYearValue'),
  );
  // 七到十二月
  fixedColumns = _.concat(
    fixedColumns,
    setFixedColumn(attributesCheckeds, year, 'secondMonthlyValue'),
  );
  return fixedColumns;
};

export const getTitleTree = (attributesCheckeds, deadline) => {
  const year = deadline.split('-')[0];
  const titleTree = [
    {
      name: '项目/分期/组团/产品构成',
      key: 'id',
      colspan: 1,
      rowspan: 4,
    },
  ];

  for (let i = 1; i <= 16; i += 1) {
    // 年度合计
    if (i === 1) {
      titleTree.push({
        name: `${year}年合计`,
        key: 'yearValue',
        rowspan: 1,
        children: [
          {
            name: '年初预算版',
            key: 'yearValue-budgetValue',
            colspan: 1 * attributesCheckeds.length,
          },
          {
            name: '最新动态版',
            key: 'yearValue-dynamicValue',
            colspan: 1 * attributesCheckeds.length,
          },
          {
            name: '差异',
            key: 'yearValue-differenceValue',
            colspan: 1 * attributesCheckeds.length,
          },
        ],
      });
    }
    // 一到六月
    if (i > 1 && i < 8) {
      titleTree.push({
        name: `${year}-0${i - 1}`,
        key: `${year}-0${i - 1}`,
        rowspan: 1,
        children: [
          {
            name: '年初预算版',
            key: `budgetValue-${year}-0${i - 1}`,
            colspan: 1 * attributesCheckeds.length,
          },
          {
            name: '动态版',
            key: `dynamicValue-${year}-0${i - 1}`,
            colspan: 1 * attributesCheckeds.length,
          },
          {
            name: '差异',
            key: `differenceValue-${year}-0${i - 1}`,
            colspan: 1 * attributesCheckeds.length,
          },
        ],
      });
    }
    // 半年度对比
    if (i === 9) {
      titleTree.push({
        name: '半年度对比',
        key: 'firstHalfYearValue',
        rowspan: 1,
        children: [
          {
            name: '年初预算版',
            key: 'firstHalfYearValue-budgetValue',
            colspan: 1 * attributesCheckeds.length,
          },
          {
            name: '供货实际',
            key: 'firstHalfYearValue-dynamicValue',
            colspan: 1 * attributesCheckeds.length,
          },
          {
            name: '差异',
            key: 'firstHalfYearValue-differenceValue',
            colspan: 1 * attributesCheckeds.length,
          },
        ],
      });
    }
    // 下半年计划
    if (i === 10) {
      titleTree.push({
        name: '下半年计划',
        key: 'secondHalfYearValue',
        rowspan: 1,
        children: [
          {
            name: '年中预算版',
            key: 'secondHalfYearValue-budgetValue',
            colspan: 1 * attributesCheckeds.length,
          },
          {
            name: '最新动态版',
            key: 'secondHalfYearValue-dynamicValue',
            colspan: 1 * attributesCheckeds.length,
          },
          {
            name: '差异',
            key: 'secondHalfYearValue-differenceValue',
            colspan: 1 * attributesCheckeds.length,
          },
        ],
      });
    }
    // 七到十二月
    if (i > 10) {
      titleTree.push({
        name: i - 4 < 10 ? `${year}-0${i - 4}` : `${year}-${i - 4}`,
        key: i - 4 < 10 ? `${year}-0${i - 4}` : `${year}-${i - 4}`,
        rowspan: 1,
        children: [
          {
            name: '年中预算版',
            key: i - 4 < 10 ? `budgetValue-${year}-0${i - 4}` : `budgetValue-${year}-${i - 4}`,
            colspan: 1 * attributesCheckeds.length,
          },
          {
            name: '动态版',
            key: i - 4 < 10 ? `dynamicValue-${year}-0${i - 4}` : `dynamicValue-${year}-${i - 4}`,
            colspan: 1 * attributesCheckeds.length,
          },
          {
            name: '差异',
            key:
              i - 4 < 10 ? `differenceValue-${year}-0${i - 4}` : `differenceValue-${year}-${i - 4}`,
            colspan: 1 * attributesCheckeds.length,
          },
        ],
      });
    }
  }
  return titleTree;
};
